package com.brillilab.service.module.message.producer;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.SendResult;
import com.brillilab.common.rocketmq.OnsMessageEnum;
import com.brillilab.common.rocketmq.OnsMessageVo;
import com.brillilab.common.rocketmq.OnsProducer;
import com.brillilab.domain.po.message.Message;
import com.brillilab.service.core.message.IMessageService;
import com.brillilab.service.module.message.enums.AppPushTypeEnum;
import com.brillilab.service.module.message.template.MessageTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Component
public class MessageProducer {

    private static Logger logger = LoggerFactory.getLogger(MessageProducer.class);

    @Resource
    private IMessageService messageService;

    @Resource
    private OnsProducer onsProducer;

    /**
     * 单个接收者消息推送
     *
     * @param messageTemplate
     * @return
     */
    public boolean sendMessagePush(MessageTemplate messageTemplate) {

        Message message = messageTemplate.buildMessage();

        if (message.getPushType() == AppPushTypeEnum.NOTICE.getName()) {
            messageService.insert(message);
        }
        OnsMessageVo onsMessageVo=null;
        if (message.getPushType().equals(AppPushTypeEnum.NOTICE.getName())) {
            onsMessageVo = messageTemplate.buildOnsMessageVo(message.getId(), OnsMessageEnum.PUSH_NOTICE.getType(), OnsMessageEnum.PUSH_NOTICE.getTitle());
        }else if (message.getPushType().equals(AppPushTypeEnum.MESSAGE.getName())) {
            onsMessageVo = messageTemplate.buildOnsMessageVo(message.getId(), OnsMessageEnum.PUSH_MESSAGE.getType(), OnsMessageEnum.PUSH_MESSAGE.getTitle());
        }

        //消息实体序列化存储
        onsMessageVo.setMessageData(JSON.toJSONString(message));

        logger.info("sendMsgData:" + onsMessageVo.getMessageData());

        if (message.getPushType().equals(AppPushTypeEnum.MESSAGE.getName())) {
            //推送消息（透传）时，只执行集群推送
            SendResult sendResult = onsProducer.sendClusteringMessage(onsProducer.getOnsProperties().appPushTopic, onsMessageVo);
            logger.info("msgSendResult-Cluster-Message:" + JSON.toJSONString(sendResult));
        }else if (message.getPushType().equals(AppPushTypeEnum.NOTICE.getName())) {
            //推送通知时，广播+集群都执行
            onsProducer.sendBroadcastMessage(onsProducer.getOnsProperties().webSocketAioTopic, onsMessageVo);
            SendResult sendResult = onsProducer.sendClusteringMessage(onsProducer.getOnsProperties().appPushTopic, onsMessageVo);
            logger.info("msgSendResult-Cluster-Notice:" + JSON.toJSONString(sendResult));
        }

        return true;
    }

    /**
     * 多个接收者消息推送
     *
     * @param messageTemplate
     * @return
     */
    public boolean sendMessagePushMutiReceiver(MessageTemplate messageTemplate) {
        List<Message> messages = messageTemplate.buildMessageList();
        messages = messageService.insertBatch(messages);
        if (!CollectionUtils.isEmpty(messages)) {
            for (Message message : messages) {
                OnsMessageVo onsMessageVo = messageTemplate.buildOnsMessageVo(message.getId(), OnsMessageEnum.PUSH_NOTICE.getType(),OnsMessageEnum.PUSH_NOTICE.getTitle());
                //消息实体序列化存储
                onsMessageVo.setMessageData(JSON.toJSONString(message));
                SendResult sendResult = onsProducer.sendBroadcastMessage(onsProducer.getOnsProperties().webSocketAioTopic, onsMessageVo);
                logger.info("msgSendResult-batch:" + JSON.toJSONString(sendResult));
            }

            OnsMessageVo onsMessageVo = messageTemplate.buildOnsMessageVo(messages.get(0).getId(),OnsMessageEnum.PUSH_NOTICE.getType(),OnsMessageEnum.PUSH_NOTICE.getTitle());
            //消息实体序列化存储
            onsMessageVo.setMessageData(JSON.toJSONString(messages.get(0)));
            SendResult sendResult = onsProducer.sendClusteringMessage(onsProducer.getOnsProperties().appPushTopic, onsMessageVo);
            logger.info("msgSendResult-Cluster-batch:" + JSON.toJSONString(sendResult));
        }

        return true;
    }
}
